//
//  SmallDecisionsSwitchView.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/6.
//

#import "SmallDecisionsSwitchView.h"

@implementation SmallDecisionsSwitchView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self createSmallDecisionsSwitchViewSubviews];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSmallDecisionsSwitchViewSubviews];
    }
    return self;
}

- (void)createSmallDecisionsSwitchViewSubviews {
    self.backgroundColor = SmallDecisionsSecondBgColor;
    [self leftLabel];
    [self rightSwitch];
}

- (UISwitch *)rightSwitch {
    if(!_rightSwitch) {
        _rightSwitch  = [[UISwitch alloc] init];
        _rightSwitch.onTintColor = SmallDecisionsThemeColor;
        [_rightSwitch addTarget:self action:@selector(switchChangeAction:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_rightSwitch];
        [_rightSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.centerY.mas_equalTo(self);
        }];
    }
    
    return _rightSwitch;
}

- (UILabel *)leftLabel {
    if(!_leftLabel) {
        _leftLabel = [[UILabel alloc] init];
        _leftLabel.numberOfLines = 2;
        _leftLabel.textColor = SmallDecisionsThemeFontColor;
        _leftLabel.font = SmallDecisionsSetFontSize(16);
        [self addSubview:_leftLabel];
        [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self);
            make.left.mas_equalTo(15);
        }];
    }
    return _leftLabel;
}

- (void)switchChangeAction:(UISwitch *)switchView {
    if (self.switchIsOnChange) {
        self.switchIsOnChange(switchView.isOn);
    }
}

@end
